var ids = getvl1("id");
var shuids = "";
var idssss = getvl1("idsss");
$(function() {
	if(ids != '') {
		xiugai(ids);
	/*	sessionStorage.setItem("goodsid", ids);*/
		/*sessionStorage.setItem("goodsids","321323");*/
	} else if(idssss == "r") {
		
		$("#fengmian").val(sessionStorage.getItem("fengmians"));
		if(sessionStorage.getItem("fengmians") == "") {
			$("#imghead1").attr("src", '../images/moren.jpg');
		} else {
			$("#imghead1").attr("src", sessionStorage.getItem("fengmians"));
		}
		var attachmentUrl1 = sessionStorage.getItem("xijietus");
		//$("#xunhuan").html("");
		if(attachmentUrl1 != null && attachmentUrl1 != '') {
			var imgarr1 = attachmentUrl1.split("***");
			var imgStr2 = '';
			$.each(imgarr1, function(index, value) {
				imgStr2 += '<li onclick="shanchu(this)"><input type="text" value="' + value + '" style="display: none;"  class="classa" id="xijietu"/><img onerror="errorImg(this)" src="' + value + '"></li>';
			});
		}
		/*imgStr2+='<li>'+
                    '<img src="../images/moren.jpg">'+
                    '<input type="file" id="xijietu" onchange="previewImage2(this)">'+
                '</li>';*/
		$("#xunhuan").append(imgStr2);
		 $("#cityResult").html(sessionStorage.getItem("goodsleibiename"));
		$("#biaoti").val(sessionStorage.getItem("biaotis")); //标题
		$("#miaoshu").val(sessionStorage.getItem("miaoshus")); //描述 
		$("#pifajia").val(sessionStorage.getItem("pifajias")); //批发价
		$("#jianhsu").val(sessionStorage.getItem("jianhsu")); //件数
		$("#shuliangs").val(sessionStorage.getItem("shuliangsss")); //拿货价
		//$("#nahuojianshu").val(sessionStorage.getItem("nahuojianshus")); //拿货件数
		$("#dabaojiage").val(sessionStorage.getItem("dabaojiages")); //打包价格
		$("#dabaojianshu").val(sessionStorage.getItem("dabaojianshus")); //打包件数
		$("#zhognliang").val(sessionStorage.getItem("zhongliangs")); //重量
		$("#tuichun").val(sessionStorage.getItem("tuijians")); //推荐
/*		$("#huiyuanguankan").val(sessionStorage.getItem("huiyuanguankans")); //会员观看 
*/		$("#banshibaohu").val(sessionStorage.getItem("banshibaohus")); //板式保护
		shuids = sessionStorage.getItem("shuids");
		$("#mingzi").html("");
		$("#mingzi").html(sessionStorage.getItem("mingzis"));
		var tui = $("#tuichun").val(); //推荐 
		if(tui == 1) {
			$("#tuijians").attr("class", "mui-switch mui-switch-mini mui-active");
			$("#tuichun").val("1");
		} else {
			$("#tuichun").val("0"); //推荐
		}
	/*	if(sessionStorage.getItem("huiyuanguankans") == 1) {
			$("#huiyaunss").attr("class", "mui-switch mui-switch-mini mui-active") //会员观看 
			$("#huiyuanguankan").val("1"); //会员观看 
		} else {
			$("#huiyuanguankan").val("0"); //会员观看 
		}*/

		if(sessionStorage.getItem("banshibaohus") == 1) {
			$("#banquanss").attr("class", "mui-switch mui-switch-mini mui-active");
			$("#banshibaohu").val("1"); //板式保护
		} else {
			$("#banshibaohu").val("0"); //板式保护
		}
		$("#sizeid").val("");
		$("#chimas").html("");
		$("#colorid").val("");
		$("#yanshes").html("");

		$("#colorid").val(sessionStorage.getItem("yansheids"));
		$("#yanshes").html(sessionStorage.getItem("yanshes"));
		$("#sizeid").val(sessionStorage.getItem("sizeids"));
		$("#chimas").html(sessionStorage.getItem("chimass"));

		if(sessionStorage.getItem("goodsids") != "" && sessionStorage.getItem("goodsids")!=null) {
			$("#xiugaifabu").html("");
			var xiugaifabu = '<input type="button" class="p_button" onclick="xiugaifabugoods(' + sessionStorage.getItem("goodsids") + ')" value="修改">';
			$("#xiugaifabu").append(xiugaifabu);
		}

	}
});

function xiugai(id) {
	$.ajax({
		url: urlcore + "/api/goods/selectOnegoods1?id=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;
				$("#biaoti").val(da.goodsTitle); //标题
				$("#miaoshu").html(da.goodsSimple); //描述 
				$("#wenleng").html(da.goodsSimple.length+"/1000");
				$("#pifajia").val(da.tradePrice); //批发价
				$("#jianhsu").val(da.tradeNumber); //件数
				// $("#nahuojia").val(da.getPrice); //拿货价
				$("#shuliangs").val(da.averageInventory);
				$("#cityResult").html(da.sortTypeName); //类别  //类别id sortTypeId
				$("#dabaojiage").val(da.packPrice); //打包价格.
				 if(da.sizeId!=null){
				 	$("#sizeid").val(da.sizeId);
				 }
				if(da.colourId!=null){
					$("#colorid").val(da.colourId);
				}
				if(da.sizeName!=null){
						$("#chimas").html(da.sizeName);
				}
			     if(da.colourName!=null){
			    		$("#yanshes").html(da.colourName);
			    }
			    if(da.sortTypeId!=null){
			    sessionStorage.setItem("goodsleibie",da.sortTypeId);
			    }else{
			    	  sessionStorage.setItem("goodsleibie","");
			    }
				
				  if(da.propertyTypeName!=null){
			   $("#mingzi").html(da.propertyTypeName);
			    }else{
			   $("#mingzi").html();
			    }
				
				
				$("#propertyTypeIds").val(da.propertyTypeId);
				shuids = da.propertyTypeId;
				$("#dabaojianshu").val(da.packNumber); //打包件数
				$("#zhognliang").val(da.weight); //重量
				$("#chisu").html(da.shop.leftRecommendTime);
				/*if(da.isMember == 1) {
					$("#huiyaunss").attr("class", "mui-switch mui-switch-mini mui-active") //会员观看 
					$("#huiyuanguankan").val("1"); //会员观看 
				} else {
					$("#huiyuanguankan").val("0"); //会员观看 
				}*/

				if(da.formatCare == 1) {
					$("#banquanss").attr("class", "mui-switch mui-switch-mini mui-active");
					$("#banshibaohu").val("1"); //板式保护
				} else {
					$("#banshibaohu").val("0"); //板式保护
				}

				var attachmentUrl1 = da.goodsUrl;
				$("#xunhuan").html("");
				if(attachmentUrl1 != null && attachmentUrl1 != '') {
					var imgarr1 = attachmentUrl1.split("***");
					var imgStr2 = '';
					$.each(imgarr1, function(index, value) {
						imgStr2 += '<li><input type="text" value="' + value + '" style="display: none;"  class="classa" id="xijietu"/><img src="' + value + '"><span data-name="delete" onclick="shanchu(this)"></span></li>';
					});
				}
				$("#xunhuan").append(imgStr2);
				$("#fengmian").val(da.coverUrl);
				$("#imghead1").attr("src", da.coverUrl);
				$("#xiugaifabu").html("");
				var xiugaifabu = '<input type="button" class="p_button" onclick="xiugaifabugoods(' + id + ')" value="修改">';
				$("#xiugaifabu").append(xiugaifabu);

				// chaxunyanshe(id);
			} else {
				if(data.msg != '空数据') {
					alert(data.msg)
				} else {
					$('#thiscount').text(0);
				}
			}

		},
		error: function() {
			alert("error");
		}
	});
}





  chimachima();
 function chimachima(){
 	  $("#chaxunchima").html("");
 	    $("#chanyongshe").html("");
	    $.ajax({
      url: urlcore+"/api/size/selectAllList3s?userId="+uuuuid,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					/*alert(JSON.stringify(data.data.sizeList))*/
					  $.each(data.data.sizeList,function(y,n){
		var chaxunchima = '<li onclick="dianjisize(this)" id="' + n.id + '">' + n.sizeName + '</li>';
					$("#chaxunchima").append(chaxunchima);
					  });
					  
					  $.each(data.data.colourList,function(q,w){
					var chanyongshe = '	<dd>' +
						'			<div id="yansheess" onclick="dianjiyanshe(this)" style="background-color: ' + w.colourValue + '; border: 1px solid #E5E5E5;">' +
						'				<i style="display: inline;" class="open">' +
						'					<img  src="" />' +
						'         <input type="text" class="" id="' + w.id + '" value="' + w.colourName + '">' +
						'				</i>' +
						'			</div>' +
						'			<h6  class="colors">' + w.colourName + '</h6>' +
						'		</dd>';
					$("#chanyongshe").append(chanyongshe);
				});
					 }
		/*console.log(goodslist);*/
			},
			error: function() {
				alert("error");
			}
		});

 }




//yanshes();


function dianjiyanshe(te) {
	var colorid = $(te).next().attr("id");
	var colourName = $(te).next().html();
	var op = $(te).children(".open").children("img").attr("src");
	if(op != "../images/color.png") {
		$(te).children(".open").children("img").attr("src", "../images/color.png");
		$(te).find("input").attr("class", "123s");
	} else {
		$(te).find("img").attr("src", "");
		$(te).find("input").attr("class", "");
	}
}

function dianjisize(te) {
	var sizeclass = $(te).attr("class");
	if(sizeclass == "p_chima_curr") {
		$(te).attr("class", "");
	} else {
		$(te).attr("class", "p_chima_curr");
	}
}

function wanchengcolore() {
	var colorname = "";
	var colorid = "";
	$("#colorid").val("");
	$("#yanshes").html("");

	$("#chanyongshe").find("input").each(function(index, item) {
		if($(this).attr("class") == "123s") {
			colorid += $(this).attr("id") + ",";
			colorname += $(this).val() + ",";
		}
	});
	var sizeidss = colorid.substring(0, colorid.length - 1);
	var sizeidsss = colorname.substring(0, colorname.length - 1);
	$("#colorid").val(sizeidss);

	if(sizeidsss!=""){
		  if(sizeidsss.length>20){
		  		$("#yanshes").html(sizeidsss.substr(0 , 20)+"...");
		  }else{
		  	$("#yanshes").html(sizeidsss);
		  }
			
	}else{
	$("#yanshes").html("如图");
	}
}

function wanchengsize() {
	var sizeid = "";
	var sizename = "";
	$("#sizeid").val("");
	$("#chimas").html("");
	$("#chaxunchima").find("li").each(function(index, item) {
		if($(this).attr("class") == "p_chima_curr") {
			sizeid += $(this).attr("id") + ",";
			sizename += $(this).html() + ",";
		}
	});
	var sizeidss = sizeid.substring(0, sizeid.length - 1);
	var sizeidsss = sizename.substring(0, sizename.length - 1);
	$("#sizeid").val(sizeidss);
	if(sizeidsss!=""){
		if(sizeidsss.length>26){
		  		$("#chimas").html(sizeidsss.substr(0 , 26)+"...");
		  }else{
		  	$("#chimas").html(sizeidsss);
		  }
	}else{
	$("#chimas").html("如图");
	}
}

function xiugaifabugoods(id) {
	var fengmian = $("#fengmian").val(); //封面
	var xijie = "";
	$(".classa").each(function(t, y) {
		xijie += $(this).val() + "***";
	});
	var xijietupian = "";
	xijietupian = xijie.substring(0, xijie.length - 3); //细节
	var biaoti = $("#biaoti").val(); //标题
	var miaoshu = $("#miaoshu").val(); //描述 
	var pifajia = $("#pifajia").val(); //批发价
	var jianhsu = $("#jianhsu").val(); //件数
	// var nahuojia=$("#nahuojia").val(); //拿货价
	//var nahuojianshu=$("#nahuojianshu").val(); //拿货件数
	var dabaojiage = $("#dabaojiage").val(); //打包价格
	var dabaojianshu = $("#dabaojianshu").val(); //打包件数
	var zhongliang = $("#zhognliang").val(); //重量
	var tuijian = $("#tuichun").val(); //推荐
	/*var huiyuanguankan = $("#huiyuanguankan").val(); //会员观看 */
	var banshibaohu = $("#banshibaohu").val(); //板式保护
	var sizeid = $("#sizeid").val(); //size尺寸id
	var colorid = $("#colorid").val(); //colordid
	var shuliangs = $("#shuliangs").val();
/*	var goodsleibie = 
	$("#goodsleibie").val();*/


//alert(shuliangs)
	if(fengmian == '') {
		alert("封面图不能空");
	} else if(xijietupian == '') {
		alert("细节图不能为空");
	} else if(biaoti == '') {
		alert("标题不能为空");
	} else if(pifajia == '') {
		alert("批发价不能为空");
	} else if(jianhsu == '') {
		alert("件数不能为空");
	} else if(dabaojiage == '') {
		alert("打包价格不能为空");
	} else if(dabaojianshu == '') {
		alert("打包件数不能为空");
	} else if(dabaojianshu == '') {
		alert("重量不能为空");
	} else if(tuijian == '') {
		alert("推荐不能为空");
	} else if(colorid == '') {
		alert("规格不能为空");
	} else if(sizeid == '') {
		alert("规格不能为空");
	} else if(shuliangs < 1) {
		alert("数量太少");
	} else if(sessionStorage.getItem("goodsleibie") == "") {
		alert("请选择类别");
	} else if(uuuuid != "") {
		$.ajax({
			url: urlcore + "/api/goods/wxupdateGoodsByPhone?userId=" + uuuuid,
			type: "post",
			contentType: "application/json;charset=utf-8",
			data: JSON.stringify({
				"id": id,
				"coverUrl": fengmian,
				"goodsUrl": xijietupian,
				"goodsTitle": biaoti,
				"goodsSimple": miaoshu,
				"tradePrice": pifajia,
				//"getPrice": nahuojia, 
				"packPrice": dabaojiage,
				"tradeNumber": jianhsu,
				"averageInventory": shuliangs,
				"packNumber": dabaojianshu,
				"weight": zhongliang,
				"isIndex": tuijian, //
				"colourId": colorid,
				"sizeId": sizeid,
				"formatCare": banshibaohu,
				"sortTypeId": sessionStorage.getItem("goodsleibie"),
				"propertyTypeId": shuids
			}),
			dataType: "json",
			success: function(data) {
				if(data.success == true) {
					alert("修改成功");
					window.location.href="24.0.0goodsguanli.html?id="+uuuuid;
					//window.location.href = document.referrer;
				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					if(thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href = "login.html";
					}
				} else {
					alert(data.msg);
				}

			},
			error: function() {
				alert("error");
			}

		});
	}
}

function dianjila() {
	  if(ids!=""){
	  	sessionStorage.setItem("goodsid",ids);
	  }else{
        	sessionStorage.setItem("goodsid","");  	  	
	  }
	
	
	
	sessionStorage.setItem("fengmian", $("#fengmian").val());
	var xijie = "";
	$(".classa").each(function(t, y) {
		xijie += $(this).val() + "***";
	});
	var xijietupian = "";
	xijietupian = xijie.substring(0, xijie.length - 3); //细节
	sessionStorage.setItem("xijietu", xijietupian);
	//标题
	sessionStorage.setItem("biaoti", $("#biaoti").val());
	//描述 
	sessionStorage.setItem("miaoshu", $("#miaoshu").val());
	//批发价
	sessionStorage.setItem("pifajia", $("#pifajia").val());
	//件数
	sessionStorage.setItem("jianhsu", $("#jianhsu").val());
	/*	 //拿货价
		  
	     //拿货件数
		  sessionStorage.setItem("shuliangs",$("#shuliangs").val());*/
	//打包价格

	

	sessionStorage.setItem("shuliangs", $("#shuliangs").val());
	sessionStorage.setItem("dabaojiage", $("#dabaojiage").val());
	//打包件数
	sessionStorage.setItem("dabaojianshu", $("#dabaojianshu").val());
	//重量
	sessionStorage.setItem("zhongliang", $("#zhognliang").val());
	//推荐
	sessionStorage.setItem("tuijian", $("#tuichun").val());

	//板式保护
	sessionStorage.setItem("banshibaohu", $("#banshibaohu").val());
	//颜色
	sessionStorage.setItem("yanshe", $("#yanshes").html());
	//颜色id。
	sessionStorage.setItem("yansheid", $("#colorid").val());
	//尺寸
	sessionStorage.setItem("sizeid", $("#sizeid").val());
	sessionStorage.setItem("chimas", $("#chimas").html());

}

/**sessionStorage.setItem("site", $("#fengmian").val());
	alert(sessionStorage.getItem("site"));*/
/* alert(sessionStorage.getItem("site"));*/

/* sessionStorage.setItem("pic",fengmian); 存入缓存*/
/*sessionStorage.setItem("site", "js8.in"); 存入本地*/

function fabushangping() {

	var fengmian = $("#fengmian").val(); //封面
	var xijie = "";
	$(".classa").each(function(t, y) {
		xijie += $(this).val() + "***";
	});
	var xijietupian = "";
	xijietupian = xijie.substring(0, xijie.length - 3); //细节
	var biaoti = $("#biaoti").val(); //标题
	var miaoshu = $("#miaoshu").val(); //描述 
	var pifajia = $("#pifajia").val(); //批发价
	var jianhsu = $("#jianhsu").val(); //件数
	//var nahuojia=$("#nahuojia").val(); //拿货价
	var shuliangs = $("#shuliangs").val(); //拿货件数
	var dabaojiage = $("#dabaojiage").val(); //打包价格
	var dabaojianshu = $("#dabaojianshu").val(); //打包件数
	var zhongliang = $("#zhognliang").val(); //重量
	var tuijian = $("#tuichun").val(); //推荐
	/*var huiyuanguankan = $("#huiyuanguankan").val(); //会员观看 */
	var banshibaohu = $("#banshibaohu").val(); //板式保护
	var sizeid = $("#sizeid").val(); //size尺寸id
	var colorid = $("#colorid").val(); //colordid

	if(fengmian == '') {
		alert("封面图不能空");
	} else if(xijietupian == '') {
		alert("细节图不能为空");
	} else if(biaoti == '') {
		alert("标题不能为空");
	} else if(pifajia == '') {
		alert("批发价不能为空");
	} else if(jianhsu == '') {
		alert("件数不能为空");
	} else if(dabaojiage == '') {
		alert("打包价格不能为空");
	} else if(dabaojianshu == '') {
		alert("打包件数不能为空");
	} else if(dabaojianshu == '') {
		alert("重量不能为空");
	} else if(tuijian == '') {
		alert("推荐不能为空");
	} else if(colorid == '') {
		alert("规格不能为空");
	} else if(sizeid == '') {
		alert("规格不能为空");
	} else if(shuliangs < 1) {
		alert("数量过小");
	} else if(sessionStorage.getItem("goodsleibie") == "") {
		alert("类别不能为空");
	} else {
		$.ajax({
			url: urlcore + "/api/goods/addGoodsByPhone12?userId=" + uuuuid,
			type: "post",
			contentType: "application/json;charset=utf-8",
			data: JSON.stringify({
				"coverUrl": fengmian,
				"goodsUrl": xijietupian,
				"goodsTitle": biaoti,
				"goodsSimple": miaoshu,
				"tradePrice": pifajia,
				//"getPrice": nahuojia, 
				"packPrice": dabaojiage,
				"tradeNumber": jianhsu,
				"averageInventory": shuliangs,
				"packNumber": dabaojianshu,
				"weight": zhongliang,
				"isIndex": tuijian,
				"formatCare": banshibaohu,
				"colourId": colorid,
				"sizeId": sizeid,
				"sortTypeId": sessionStorage.getItem("goodsleibie"),
				"propertyTypeId": shuids
			}),
			dataType: "json",
			success: function(data) {
				if(data.success == true) {
					alert("发布成功，待审核");
					window.location.href="20.0.0sell_wode.html";
				/*  $(".public-red").html("");
					$(".public-red").html('<a href="20.0.0sell_wode.html" class="p_header_left">'+
                      '<i class="p_left" title="返回"></i>'+
                      ' </a>'+
                     ' <h1 class="p_dizhi">商品发布</h1>');*/
                     
				}else{
					alert(data.msg);
				}
			},
			error: function() {
				alert("error");
			}

		});

	}
}





function addNew2(){
	   if(uuuuid!=""){
     	var sizeName=$("#sizeName").val();
	          if(sizeName.length<100){
	        if(sizeName!=""){
    $.ajax({
        url:  urlcore +"/api/size/addNew3",
        type: "post",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
        	    userId:uuuuid,
        	   sizeName:sizeName
        }),
        dataType: "json",
        success:function(data){
            if (data.success == true) {
            	  chimachima();
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "1.0.0denglu.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error: function() {
            alert("error");
        }
      });
	          	}
	          }else{
	          	alert("标题过长");
	          }
	   }else{
	   	alert("未登录/或登录超时");
	   }
}



function addNew(){
	   if(uuuuid!=""){
	  
     	var Newcolor=$("#Newcolor").val();
     	var zhi=$(".sp-input").val();
	          if(Newcolor.length<100){
	           if(Newcolor!=""){
	           	
    $.ajax({
        url:  urlcore +"/api/colour/addNew3",
        type: "post",
        contentType: "application/json;charset=utf-8", /*  colourValue:zhi*/
        data: JSON.stringify({
        	    userId:uuuuid,
        	   colourName:Newcolor,
        	 
        }),
        dataType: "json",
        success:function(data){
            if (data.success == true) {
            	  chimachima(uuuuid);
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "1.0.0denglu.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error: function() {
            alert("error");
        }
    });
	          	  	
	       /*   	if(zhi!=""){}else{
	          	  		alert("未选择颜色编码");
	          	  	}*/
	          	  }else{
	          	  	alert("标题不能为空");
	          	  }
	          }else{
	          	alert("标题过长");
	          	
	          }
	          
	           	
	   }else{
	   	alert("未登录/或登录超时");
	   }
   	    
}
